﻿namespace NServiceBus.PersistenceTesting.Sagas;

using System;
using System.Threading.Tasks;
using NUnit.Framework;

public class When_retrieving_the_same_saga_twice : SagaPersisterTests
{
    [Test]
    public async Task Get_returns_different_instance_of_saga_data()
    {
        var correlationPropertyData = Guid.NewGuid().ToString();
        var saga = new TestSagaData
        {
            SomeId = correlationPropertyData,
            DateTimeProperty = DateTime.UtcNow
        };

        await SaveSaga(saga);

        var returnedSaga1 = await GetById<TestSagaData>(saga.Id);
        var returnedSaga2 = await GetById<TestSagaData>(saga.Id);

        using (Assert.EnterMultipleScope())
        {
            Assert.That(returnedSaga1, Is.Not.SameAs(returnedSaga2));
            Assert.That(saga, Is.Not.SameAs(returnedSaga1));
        }

        Assert.That(saga, Is.Not.SameAs(returnedSaga2));
    }

    public class TestSaga : Saga<TestSagaData>, IAmStartedByMessages<StartMessage>
    {
        public Task Handle(StartMessage message, IMessageHandlerContext context) => throw new NotImplementedException();

        protected override void ConfigureHowToFindSaga(SagaPropertyMapper<TestSagaData> mapper) => mapper.MapSaga(s => s.SomeId).ToMessage<StartMessage>(msg => msg.SomeId);
    }

    public class StartMessage
    {
        public string SomeId { get; set; }
    }

    public class TestSagaData : ContainSagaData
    {
        public string SomeId { get; set; } = "Test";

        public DateTime DateTimeProperty { get; set; }
    }

    public When_retrieving_the_same_saga_twice(TestVariant param) : base(param)
    {
    }
}